import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'package:flutter_elf/utils/asset_utils.dart';
import 'package:flutter_elf/utils/get_utils.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path_provider/path_provider.dart';
import '../download_data.dart';
import 'file_utils.dart';
import 'dart:isolate';
import 'package:http/http.dart' as http;
import 'package:convert/convert.dart' show json;

import '../gank_girl_response.dart';
String girls = '''
{
  "data":[
  {"pictureUrl":"http://gank.io/images/1af9d69bc60242d7aa2e53125a4586ad"},
  {"pictureUrl":"http://gank.io/images/b90db096a8a14e86a373222e87917c68"},
  {"pictureUrl":"http://gank.io/images/95207edc08ed4cf0890b4cc4a02ccac9"},
  {"pictureUrl":"http://gank.io/images/7fa98787d009465a9d196fbff6b0a5d7"},
  {"pictureUrl":"http://gank.io/images/3e4423173d0a4c5e8447c0335b4458fc"},
  {"pictureUrl":"http://gank.io/images/aebca647b3054757afd0e54d83e0628e"},
  {"pictureUrl":"http://gank.io/images/89c170bf253346d98240d456cbc1c688"},
  {"pictureUrl":"http://gank.io/images/02eb8ca3297f4931ab64b7ebd7b5b89c"},
  {"pictureUrl":"http://gank.io/images/3a1997f4ecfb4a65ad84692fea3123bf"},
  {"pictureUrl":"http://gank.io/images/2e75774eac3f497caca35b3de7c50a42"},
  {"pictureUrl":"http://gank.io/images/ce66aa74d78f49919085b2b2808ecc50"},
  {"pictureUrl":"http://gank.io/images/28ff615f01f5400f97a7ce6c085ddf11"},
  {"pictureUrl":"http://gank.io/images/aa3be1436248473996790a89f6583e04"},
  {"pictureUrl":"http://gank.io/images/2b713742032c4b38a604389a22285a53"},
  {"pictureUrl":"http://gank.io/images/31f92f7845f34f05bc10779a468c3c13"},
  {"pictureUrl":"http://gank.io/images/1a515f1508e345e2bf24673c2c2d50c4"},
  {"pictureUrl":"http://gank.io/images/f9523ebe24a34edfaedf2dd0df8e2b99"},
  {"pictureUrl":"http://gank.io/images/2c924db2a1b84c5d8fdb9f8c5f6d1b71"},
  {"pictureUrl":"http://gank.io/images/4002b1fd18544802b80193fad27eaa62"},
  {"pictureUrl":"http://gank.io/images/5a29ab0fc093408c82febe7c7e42e156"},
  {"pictureUrl":"http://gank.io/images/a2d3115b8d464d93933e79e88af03580"},
  {"pictureUrl":"http://gank.io/images/fae111696a8b418297833324ff93bd1a"},
  {"pictureUrl":"http://gank.io/images/c6d3b2b6b5e24e1cbf576946dbec5907"},
  {"pictureUrl":"http://gank.io/images/e941fa5d2cfb4a8297128178c371908c"},
  {"pictureUrl":"http://gank.io/images/28fc02e86d584ff08802c8dcd9535b35"},
  {"pictureUrl":"http://gank.io/images/8a9837115fb64d22b0484e3d4c4cab50"},
  {"pictureUrl":"http://gank.io/images/5888858f49bd4608b12633115687ddc3"},
  {"pictureUrl":"http://gank.io/images/65c8ea426cc7423987692872968c25b9"},
  {"pictureUrl":"http://gank.io/images/1c5cebd307fd49eaa75b368b11118b61"},
  {"pictureUrl":"http://gank.io/images/6368ae2770414162819aba90f251cf08"},
  {"pictureUrl":"http://gank.io/images/e831e004436f4fffb657a77aef48b9ca"},
  {"pictureUrl":"http://gank.io/images/6f1708af33d647f3809ea551e18894ce"},
  {"pictureUrl":"http://gank.io/images/341ecaacdd7d4cc09cd0de100f3eab46"},
  {"pictureUrl":"http://gank.io/images/7878d08eb776401a85deeb203372665c"},
  {"pictureUrl":"http://gank.io/images/7f64754ca07e4af3a242399fd37c2432"},
  {"pictureUrl":"http://gank.io/images/cdd7031fa92d40e18a715035b686b4c4"},
  {"pictureUrl":"http://gank.io/images/58389e1189534e1cb75b7a788f6b8a86"},
  {"pictureUrl":"http://gank.io/images/9770422c45294684af50f725049d7c07"},
  {"pictureUrl":"http://gank.io/images/54fc1a56dbc44b52b23714030a457a1b"},
  {"pictureUrl":"http://gank.io/images/0fdac44dada5489b85049a3f3fb7fd85"},
  {"pictureUrl":"http://gank.io/images/79f717dc495645dfb4e9c43f4674fa30"},
  {"pictureUrl":"http://gank.io/images/e6b78c1949d5438fa37ff2f272e5f1d0"},
  {"pictureUrl":"http://gank.io/images/a0388326c2b04aa1a58aa956276c40e3"},
  {"pictureUrl":"http://gank.io/images/e343db89daff4937b28e82a275024cf7"},
  {"pictureUrl":"http://gank.io/images/367c59cb861044e4af2835b2d46988d0"},
  {"pictureUrl":"http://gank.io/images/624ade89f93f421b8d4e8fafd86b1d8d"},
  {"pictureUrl":"http://gank.io/images/b140f015a16e444aad6d76262f676a78"},
  {"pictureUrl":"http://gank.io/images/b0f73f9527694f44b523ff059d8a8841"},
  {"pictureUrl":"http://gank.io/images/882afc997ad84f8ab2a313f6ce0f3522"},
  {"pictureUrl":"http://gank.io/images/e0088b6b0773408bace28e102af9f8ee"},
  {"pictureUrl":"http://gank.io/images/be3c70b17f364596b25f301a55164882"},
  {"pictureUrl":"http://gank.io/images/5ba77f3415b44f6c843af5e149443f94"},
  {"pictureUrl":"http://gank.io/images/bdd1fb6886744bc9838a1b85495a99f7"},
  {"pictureUrl":"http://gank.io/images/5cf42ab5cb7a41aca3c0f5188f5690a5"},
  {"pictureUrl":"http://gank.io/images/e56da642238a43c4971f12d4e3395f30"},
  {"pictureUrl":"http://gank.io/images/04d6b8ee9e964c04877da65b43952e89"},
  {"pictureUrl":"http://gank.io/images/50e3dd62dd9e4b0db957b3c5e296d6b1"},
  {"pictureUrl":"http://gank.io/images/21c417e3a84842bfa585dee686f6b992"},
  {"pictureUrl":"http://gank.io/images/f8c5ce6fd1b94686a68a7d74f552cf65"},
  {"pictureUrl":"http://gank.io/images/94c30acee68340278dfc58e948109ab9"},
  {"pictureUrl":"http://gank.io/images/59019eedff4540cd88c96cb96937465f"},
  {"pictureUrl":"http://gank.io/images/30f70718ee274bb399a1069f9eb79ead"},
  {"pictureUrl":"http://gank.io/images/4d2825dcb5d04d149e6a3620833449c4"},
  {"pictureUrl":"http://gank.io/images/d449165e9f434a60afafa47bd4167d57"},
  {"pictureUrl":"http://gank.io/images/848c46c369174224aab4a80cab2b3261"},
  {"pictureUrl":"http://gank.io/images/1d47d22e27884f7cac2a7e88a38993bf"}
]
}
''';

main()async{
//  File file = File('./static/data/girls.json');
//  String string = await FileUtils.readJsonFile(file.path);
//  DownloadData data = DownloadData.fromJson(json.decode(girls));
//  bool isDirector = await FileUtils.isDirector('D:\\测试');
//  print(isDirector);
//  data.data.forEach((item)async{
//    print('开始===>${getName(item.pictureUrl)}');
//    await FileUtils.downloadFileByUrl(item.pictureUrl, 'D:\\测试\\${getName(item.pictureUrl)}.jpg');
//    print('完成===>${getName(item.pictureUrl)}');
//  });//http://gank.io/images/1af9d69bc60242d7aa2e53125a4586ad
//  await FileUtils.downloadFileByUrl('https://ae01.alicdn.com/kf/U6cdabb09d259457291dee1102d5d7a22i.jpg', 'D:\\测试\\1.jpg');

  String s = await HttpUtils.get('https://gank.io/api/v2/data/category/Girl/type/Girl/page/1/count/100');
  String s2 = await HttpUtils.get('https://gank.io/api/v2/data/category/Girl/type/Girl/page/2/count/100');
  GankGirlResponse g1 = GankGirlResponse.fromJson(json.decode(s));
  GankGirlResponse g2 = GankGirlResponse.fromJson(json.decode(s2));
  List<GankGirlData> datas = []..addAll(g1.data)..addAll(g2.data);

  count = datas.length;
  rcvPort = ReceivePort();
  download(datas);
  rcvPort.listen((msg){
    print('开始下个任务$msg');
    download(datas);
  });
  await Future.delayed(Duration(seconds: 1000));
}
int index = 0;
int count;
ReceivePort rcvPort;
download(List<GankGirlData> url)async{
  print('开始下载' + url[index].url);
  await FileUtils.downloadFileByUrl(url[index].url, 'E:\\测试3\\${getName(url[index].url)}.jpg');
  print('下载完成');
  index++;
  Isolate.spawn(port, rcvPort.sendPort);
}

port(SendPort sendPort){
  sendPort.send(index);
}

down(List<DownloadItem> items){
  List<Future> fs = items.map((i)async{
    return await FileUtils.downloadFileByUrl(i.pictureUrl, 'D:\\测试\\${getName(i.pictureUrl)}.jpg');
  }).toList();
  Future.wait(fs).then((value){
    print(value.toString());
  });
}

String getName(String str){
  return str.substring(str.lastIndexOf('/')+1);
}